package ThreadDemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-10-15
 * Time: 10:36
 */
public class Demo14 {
    public static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                //对count指令操作进行加锁
                synchronized(Object.class){//()里面传递锁锁对象
                    count++;
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                //对count指令操作进行加锁
                synchronized(Object.class){//()里面传递锁锁对象
                    count++;
                }

            }
        });

        thread1.start();
        thread2.start();
        thread1.join();//让main线程等待thread1的线程任务执行完毕，才能接受操作系统线程调度
        thread2.join();//让main线程等待thread2的线程任务执行完毕，才能接受操作系统线程调度

        System.out.println("count = " + count);
        //打印出0，代表其他线程还没开始执行任务，main主线程就提前结束
    }
}
